package io.chenglicun.common.utils;

import cn.hutool.core.collection.CollUtil;
import io.chenglicun.common.exception.RenException;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class PinYinUtils {
    /**
     * 返回拼音类型：全拼
     */
    public static final String RET_PINYIN_TYPE_FULL = "full";

    /**
     * 返回拼音类型：首字母
     */
    public static final String RET_PINYIN_TYPE_HEADCHAR = "headChar";

    /**
     * 字符串转换为拼音
     *
     * @param src     需要转换的字符串
     * @param retType 返回拼音结果类型
     */
    public static String str2Pinyin(String src, String retType) {
        if (src != null && !"".equalsIgnoreCase(src.trim())) {
            char[] srcChar = src.toCharArray();
            // 汉语拼音格式输出类
            HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();

            // 输出设置，大小写，音标方式等
            hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

            String[][] temp = transformData(src, retType, srcChar, hanYuPinOutputFormat);
            String[] pingyinArray = exchange(temp);
            Set<String> pinyinSet = new HashSet<>();
            Collections.addAll(pinyinSet, pingyinArray);
            return CollUtil.join(pinyinSet, ",");
        }
        return null;
    }

    private static String[][] transformData(String src, String retType, char[] srcChar,
                                            HanyuPinyinOutputFormat hanYuPinOutputFormat) {
        String[][] temp = new String[src.length()][];
        for (int i = 0; i < srcChar.length; i++) {
            try {
                temp[i] = PinyinHelper.toHanyuPinyinStringArray(srcChar[i], hanYuPinOutputFormat);
                //如果str.charAt(i)非汉字，则保持原样
                if (temp[i] == null) {
                    temp[i] = new String[]{String.valueOf(srcChar[i])};
                } else {
                    //如果retType是RET_PINYIN_TYPE_HEADCHAR，则只取转换后的首字母
                    if (RET_PINYIN_TYPE_HEADCHAR.equalsIgnoreCase(retType)) {
                        String[] tempTemp = new String[temp[i].length];
                        for (int j = 0; j < temp[i].length; j++) {
                            tempTemp[j] = String.valueOf(temp[i][j].charAt(0));
                        }
                        temp[i] = tempTemp;
                    }
                }

            } catch (BadHanyuPinyinOutputFormatCombination e) {
               throw new RenException(e.getMessage());
            }
        }
        return temp;
    }

    /**
     * 递归
     */
    private static String[] exchange(String[][] strJaggedArray) {
        String[][] temp = doExchange(strJaggedArray);
        return temp[0];
    }

    /**
     * 递归
     */
    private static String[][] doExchange(String[][] strJaggedArray) {
        int len = strJaggedArray.length;
        if (len >= 2) {
            int len1 = strJaggedArray[0].length;
            int len2 = strJaggedArray[1].length;
            int newLen = len1 * len2;
            String[] temp = new String[newLen];
            int index = 0;
            for (int i = 0; i < len1; i++) {
                for (int j = 0; j < len2; j++) {
                    temp[index] = strJaggedArray[0][i] + strJaggedArray[1][j];
                    index++;
                }
            }
            String[][] newArray = new String[len - 1][];
            System.arraycopy(strJaggedArray, 2, newArray, 1, len - 2);
            newArray[0] = temp;
            return doExchange(newArray);
        } else {
            return strJaggedArray;
        }
    }
}
